package pl.edu.agh.jemo.gui.panels.algorithm;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pl.edu.agh.jemo.evolution.algorithm.impl.SPEA2Algorithm;
/**
 * Configuration panel allowing to adjust SPEA2 algorithm parameters.
 * As an extension of JPanel, it can be displayed by main window, whenever it is needed.
 * It works on existing algorithm instance, using reference passed in constructor.
 * 
 * @author Marcin Zbijowski
 *
 */
public class SPEA2ConfigurationPanel extends JPanel {

	/**
	 * As JPanel is implementation of Serializable interface, it requires this field to be set.
	 */
	private static final long serialVersionUID = 7783015028383924070L;

	/**
	 * Reference to algorithm instance configured by panel.
	 */
	protected final SPEA2Algorithm algorithm;

	/**
	 * Field allowing to set expected archive size.
	 */
	protected JTextField archiveSizeField;
	/**
	 * Field allowing to set number of neighbours taken into account when crowding population.
	 */
	protected JTextField nieghbourField;
	/**
	 * Field allowing to modify hillclimbing multiplier, used by spea2.
	 */
	protected JTextField multiplierField;

	/**
	 * Creates Panel instance associated with given algorithm reference.
	 * Initializes all graphic components, so after constructor no further initialization is needed.
	 * 
	 * @param speaAlgorithm reference to spea2 algorithm, that needs configuration. 
	 */
	public SPEA2ConfigurationPanel(SPEA2Algorithm speaAlgorithm) {
		super();
		algorithm = speaAlgorithm;

		setLayout(null);

		JLabel archiveLabel = new JLabel("Archive size:");
		archiveLabel.setBounds(10, 10, 150, 15);
		add(archiveLabel);

		archiveSizeField = new JTextField(String.valueOf(algorithm
				.getArchiveSize()));
		archiveSizeField.setBounds(10, 27, 150, 22);
		add(archiveSizeField);

		archiveSizeField.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					algorithm.setArchiveSize(Integer.parseInt(archiveSizeField
							.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong integer value");
				}
			}
		});

		JLabel multiplierLabel = new JLabel("Hillclimbing multiplier:");
		multiplierLabel.setBounds(10, 55, 150, 15);
		add(multiplierLabel);

		multiplierField = new JTextField(String.valueOf(algorithm
				.getHillClimbingMultiplier()));
		multiplierField.setBounds(10, 72, 150, 22);
		add(multiplierField);

		multiplierField.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				try {
					algorithm.setHillClimbingMultiplier(Double
							.parseDouble(multiplierField.getText()));
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Wrong double value");
				}
			}
		});
	}

}
